/*
* $Id: QBeePointView.cpp 154 2010-09-27 08:10:48Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "QBeePointView.h"
#include "Components/Journal/FamilyManager.h"
#include "Components/Journal/QueenBeeManager.h"
#include "Event/EventId.h"

CQBeePointView::CQBeePointView(CState* pState)
: CQBeeView(pState){
	IEventManager::GetEventManager()->SubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->SubscribeEvent(this, FAMILIES_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->SubscribeEvent(this, QUEEN_BEE_DB_CHANGE_EVENT);
	Init();
}
CQBeePointView::~CQBeePointView(){
	IEventManager::GetEventManager()->UnSubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->UnSubscribeEvent(this, FAMILIES_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->UnSubscribeEvent(this, QUEEN_BEE_DB_CHANGE_EVENT);
}

void CQBeePointView::SetState(){
	m_pState->m_nState = Journal::VIEW_POINT_QBEE;
	m_pState->m_nIdParameter = 0;
}

bool CQBeePointView::IsViewState(State state){
	return state.m_nPosTab == Journal::QUEEN_BEE_JOURNAL &&
		state.m_type == Journal::NONE &&
		state.m_nTypeSel == Journal::NOTSEL;
}

void CQBeePointView::Init(){
	m_infoDrawer.Clear();
	std::map<int, CFamilyManager::family> aFamilies = IJournalComponent::GetFamilyManager()->GetFamilies();
	std::map<int, int> aPontsExist;
	std::map<int, int> aPointNon;
	for(std::map<int, CFamilyManager::family>::iterator it = aFamilies.begin(); it != aFamilies.end(); it++){
		std::map<int, int>::iterator itTemp = aPointNon.find(it->first);
		if(itTemp == aPointNon.end())
			aPointNon.insert(std::make_pair(it->first,  0));
		itTemp = aPontsExist.find(it->first);
		if(itTemp == aPontsExist.end())
			aPontsExist.insert(std::make_pair(it->first,  0));

		std::map<int, CQueenBeeManager::queen_bee> aQB = IJournalComponent::GetQueenBeeManager()->GetFamilyQB(it->first);
		bool bFind = false;
		for(std::map<int, CQueenBeeManager::queen_bee>::iterator it1 = aQB.begin(); it1 != aQB.end(); it1++){
			if(!it1->second.s_bDead){
				aPontsExist[it->second.m_nIdPoint]++;
				bFind = true;
			}
		}
		if(!bFind)
			aPointNon[it->second.m_nIdPoint]++;
	}
	std::map<int, CFamilyManager::point> aPoints = IJournalComponent::GetFamilyManager()->GetPoints();
	for(std::map<int, CFamilyManager::point>::iterator it = aPoints.begin(); it != aPoints.end(); it++){
		int j = m_infoDrawer.AddItem(it->first);
		m_infoDrawer.AddSubItem(j, InfoItem(it->second.m_strName, _T("Arial"), 20, true, true));
		std::tstring strTemp(ILanguageManager::GetLanguageManager()->GetString(INFO_POINT_NON_QB_ID) + _T(" - "));
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + CConvertFunction::ConvertFromInt(aPointNon[it->first]), _T("Arial"), 14, false, false));
		strTemp = ILanguageManager::GetLanguageManager()->GetString(INFO_POINT_ALL_QB_ID) + _T(" - ");
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + CConvertFunction::ConvertFromInt(aPontsExist[it->first]), _T("Arial"), 14, false, false));
	}
}

void CQBeePointView::EventHandler(){
	Init();
	if(IsCreate())
		Invalidate(FALSE);
}